package com.itbaizhan;
/**使用StringBuilder进行字符串的拼接
 * 注：字符串拼接就用StringBuilder*/
public class TestStringBuilder {
    public static void main(String[] args) {
        String str2 = "";

        long num1 = Runtime.getRuntime().freeMemory();//系统剩空间
        long time1 = System.currentTimeMillis();
        for(int i=0;i<1000;i++){
            str2 = str2+i;
        }
        long num2 = Runtime.getRuntime().freeMemory();
        long time2 = System.currentTimeMillis();

        System.out.println("占用内存："+(num1-num2));
        System.out.println("占用时间："+(time2-time1));

        StringBuilder st = new StringBuilder();

        long num3 = Runtime.getRuntime().freeMemory();
        long time3 = System.currentTimeMillis();
        for(int i=0;i<1000;i++){
            st.append(i);
        }
        long num4 = Runtime.getRuntime().freeMemory();
        long time4 = System.currentTimeMillis();
        System.out.println("占用内存："+(num3-num4));
        System.out.println("占用时间："+(time4-time3));
    }
}
